package com.zzs.elm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zzs.elm.po.Cart;
import com.zzs.elm.service.CartService;

public class CartController {
	private final CartService cartService = new CartService();

	public Object saveCart(HttpServletRequest request) throws Exception {
		Cart cart = new Cart();
		cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
		cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		cart.setUserId(request.getParameter("userId"));
		int result = cartService.saveCart(cart);
		return result;
	}

	public Object updateCart(HttpServletRequest request) throws Exception {
		Cart cart = new Cart();
		cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
		cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		cart.setUserId(request.getParameter("userId"));
		cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
		int result = cartService.updateCart(cart);
		return result;
	}

	public Object removeCart(HttpServletRequest request) throws Exception {
		Cart cart = new Cart();
		cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
		cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		cart.setUserId(request.getParameter("userId"));
		int result = cartService.removeCart(cart);
		return result;
	}

	public Object listCart(HttpServletRequest request) throws Exception {
		Cart cart = new Cart();
		cart.setUserId(request.getParameter("userId"));
		if (request.getParameter("businessId") != null) {
			cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		}
		List<Cart> list = cartService.listCart(cart);
		return list;
	}
}
